#!/usr/bin/perl
# commit_create_db_completed.pm
# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

package CCNCore::spool::commit_create_db_completed;
use strict; use warnings;
use CCNCore::Provisioning::Local;
use CCNCore::Provision::Distributed;
use CCNCore::IPC::Ticket;

our $D;

sub run
{
    my $ticket_id = shift;

    # Clear the 'commit-create-db' ticket
    my $t1 = new CCNCore::IPC::Ticket($ticket_id);
    my $system_pk = $t1->retrieve_context('system-pk');
    $t1->completed();
    $t1->clear_ticket();
    undef $t1;

    # Create the 'commit-post-db' ticket
    my $t = new CCNCore::IPC::Ticket;
    $t->set_label("Starting post-database services on system $system_pk");
    $t->add_context('system-pk',$system_pk);

    my $p = new CCNCore::Provisioning::Local;
    my $s = new CCNCore::Provision::Distributed($p);

    $s->set_logger($D);

    our $server_count = 0;

    $s->sql_do_for(<<'SQL',$system_pk,
        SELECT pk FROM server WHERE system = ?
SQL
    sub
    {
        my $server_pk = shift;
        $s->spool_this($server_pk,$t->ticket,'commit-post-db');
        $server_count++;
    });
    $t->add_action($server_count,'commit-post-db-completed');
}

1;
